<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>gmmsamp.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>data&nbsp;</span>=&nbsp;<span class=defun_name>gmmsamp</span>(<span class=defun_in>model,num_data</span>)<br>
<span class=h1>%&nbsp;GMMSAMP&nbsp;Generates&nbsp;sample&nbsp;from&nbsp;Gaussian&nbsp;mixture&nbsp;model.</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;gmmsamp(model,num_data)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;This&nbsp;function&nbsp;generates&nbsp;num_data&nbsp;samples&nbsp;from&nbsp;a&nbsp;Gaussian&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;mixture&nbsp;given&nbsp;by&nbsp;structure&nbsp;model.&nbsp;It&nbsp;returnes&nbsp;samples&nbsp;X&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;and&nbsp;a&nbsp;vector&nbsp;y&nbsp;of&nbsp;Gaussian&nbsp;component&nbsp;responsible&nbsp;for&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;generating&nbsp;corresponding&nbsp;sample.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;model</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Mean&nbsp;[dim&nbsp;x&nbsp;ncomp]&nbsp;Mean&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Cov&nbsp;[dim&nbsp;x&nbsp;dim&nbsp;x&nbsp;ncomp]&nbsp;Covariance&nbsp;matrices.&nbsp;In&nbsp;the&nbsp;case&nbsp;of&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;univariate&nbsp;mixture&nbsp;(dim=0)&nbsp;the&nbsp;variances&nbsp;can&nbsp;enter&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;as&nbsp;a&nbsp;vector&nbsp;Cov=[var1&nbsp;var2&nbsp;...&nbsp;var_ncomp].</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Prior&nbsp;[ncomp&nbsp;x&nbsp;1]&nbsp;Weighting&nbsp;coefficients&nbsp;of&nbsp;Gaussians.</span><br>
<span class=help>%&nbsp;&nbsp;num_data&nbsp;[int]&nbsp;Number&nbsp;of&nbsp;samples.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;data.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Generated&nbsp;sample&nbsp;data.</span><br>
<span class=help>%&nbsp;&nbsp;data.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Identifier&nbsp;of&nbsp;Gaussian&nbsp;which&nbsp;generated&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;given&nbsp;vector.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;struct('Mean',[-2&nbsp;3],'Cov',[1&nbsp;0.5],'Prior',[0.4&nbsp;0.6]);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;hold&nbsp;on;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;plot([-4:0.1:5],&nbsp;pdfgmm([-4:0.1:5],model),'r');</span><br>
<span class=help>%&nbsp;&nbsp;sample&nbsp;=&nbsp;gmmsamp(model,500);</span><br>
<span class=help>%&nbsp;&nbsp;[Y,X]&nbsp;=&nbsp;hist(sample.X,10);</span><br>
<span class=help>%&nbsp;&nbsp;bar(X,Y/500);</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;PDFGMM,&nbsp;GSAMP.</span><br>
<span class=help>%</span><br>
<hr>
<br>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;28-apr-2004,&nbsp;VF</span><br>
<br>
<hr>
<span class=comment>%&nbsp;allow&nbsp;input&nbsp;argument&nbsp;to&nbsp;be&nbsp;cell&nbsp;array</span><br>
model&nbsp;=&nbsp;c2s(&nbsp;model);<br>
<br>
<span class=comment>%&nbsp;get&nbsp;dimensions</span><br>
[dim,ncomp]=size(model.Mean);<br>
<br>
<span class=comment>%&nbsp;univariate&nbsp;variances&nbsp;can&nbsp;be&nbsp;given&nbsp;as&nbsp;a&nbsp;vector</span><br>
<span class=keyword>if</span>&nbsp;size(model.Cov,1)&nbsp;~=&nbsp;size(model.Cov,2),&nbsp;<br>
&nbsp;&nbsp;model.Cov&nbsp;=&nbsp;reshape(model.Cov,1,1,ncomp);<br>
<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;generates&nbsp;randomly&nbsp;Gaussian&nbsp;componenents&nbsp;responsible</span><br>
<span class=comment>%&nbsp;for&nbsp;generation&nbsp;of&nbsp;samples</span><br>
cump&nbsp;=&nbsp;repmat(cumsum(&nbsp;model.Prior(:)&nbsp;),1,num_data);<br>
rnd&nbsp;=&nbsp;ones(ncomp,1)*rand(1,num_data);<br>
<br>
data.y&nbsp;=&nbsp;ncomp-sum(cump&nbsp;&gt;&nbsp;rnd,&nbsp;1)+1;<br>
data.X&nbsp;=&nbsp;zeros(dim,num_data);<br>
<br>
<span class=comment>%&nbsp;generate&nbsp;samples</span><br>
<span class=keyword>for</span>&nbsp;i=1:ncomp,<br>
&nbsp;&nbsp;inx&nbsp;=&nbsp;find(data.y==i);<br>
&nbsp;&nbsp;data.X(:,inx)&nbsp;=&nbsp;gsamp(model.Mean(:,i),model.Cov(:,:,i),length(inx));<br>
<span class=keyword>end</span><br>
<br>
<span class=jump>return</span>;<br>
</code>
